package LeetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LC_385_MiniParser {

    public static void main(String[] args) {

    }

    public static class NestedInteger {
        NestedInteger() {
        }

        NestedInteger(int value) {
        }

        public boolean isInteger() {
            return true;
        }

        public Integer getInteger() {
            return 0;
        }

        public void setInteger(int value) {
        }

        void add(NestedInteger ni) {
        }

        public List<NestedInteger> getList() {
            return new ArrayList<>();
        }
    }

    static class Solution {
        public NestedInteger deserialize(String s) {
            Stack<NestedInteger> stack = new Stack<>();
            StringBuilder temp = new StringBuilder();
            for (char c : s.toCharArray()) {
                switch (c) {
                    case '[':
                        stack.push(new NestedInteger());
                        break;
                    case ']':
                        if (!temp.toString().equals("")) {
                            stack.peek().add(new NestedInteger(Integer.parseInt(temp.toString())));
                            temp = new StringBuilder();
                        }
                        NestedInteger top = stack.pop();
                        if (!stack.empty()) {
                            stack.peek().add(top);
                        } else {
                            return top;
                        }
                        break;
                    case ',':
                        if (!temp.toString().equals("")) {
                            stack.peek().add(new NestedInteger(Integer.parseInt(temp.toString())));
                            temp = new StringBuilder();
                        }
                        break;
                    default:
                        temp.append(c);
                }
            }
            if (!temp.toString().equals("")) {
                return new NestedInteger(Integer.parseInt(temp.toString()));
            }
            return null;
        }

    }

}